/**
 * 
 */
package io.dora.zk;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

/**
 * @author zhaohongbo
 * 
 */
public class ZookeeperClientTest {

	private ZooKeeper zk;

	public void createInstance() throws Exception {
		zk = new ZooKeeper("localhost:2181", 3000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println(event.toString());
			}
		});
	}

	public void operation() throws Exception {
		System.out
				.println("/n1. 创建 ZooKeeper 节点 (znode ： zoo2, 数据： myData2 ，权限： OPEN_ACL_UNSAFE ，节点类型： Persistent");
		zk.create("/zoo2", "myData2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
		System.out.println("/n2. 查看是否创建成功： ");
		System.out.println(new String(zk.getData("/zoo2", false, null)));
		System.out.println("/n3. 修改节点数据 ");
		zk.setData("/zoo2", "shenlan211314".getBytes(), -1);
		System.out.println("/n4. 查看是否修改成功： ");
		System.out.println(new String(zk.getData("/zoo2", false, null)));
		System.out.println("/n5. 删除节点 ");
		zk.delete("/zoo2", -1);
		System.out.println("/n6. 查看节点是否被删除： ");
		System.out.println(" 节点状态： [" + zk.exists("/zoo2", false) + "]");
		System.out.println(new String(zk.getData("/myjsf", false, null)));
		
	}
	
	
	public void getZkData() throws Exception {
		System.out.println(new String(zk.getData("/jshop-cx/com.jd.jshop.rpc.face.HelloRpc", false, null)));
		System.out.println(new String(zk.getData("/jshop-cx/com.jd.jshop.rpc.face.HelloService", false, null)));
	}
	
	private void close() throws InterruptedException {
		zk.close();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ZookeeperClientTest zct = new ZookeeperClientTest();
		try {
			zct.createInstance();
			Thread.sleep(2000);
			zct.operation();
			zct.getZkData();
			zct.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
